//
//  TTChatView.m
//  SmartTalk
//
//  Created by 陈勃 on 15-1-9.
//  Copyright (c) 2015年 gameabc. All rights reserved.
//

#import "TTChatView.h"
#import "UIView+POViewFrameBuilder.h"
#define HEIGHT_INPUT 50
#define HEIGHT_EMOJI 216
#define HEIGHT_MORE 86
#define MAX_INPUT_HEIGHT 50

@interface TTChatView ()
{
    BOOL isuiinit;
    UITextField *inputField;
    CGFloat heightBoard;
    int chatstate;
}
@end
@implementation TTChatView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)sendAction:(id)sender
{
    [inputField resignFirstResponder];
    if (_chatdelegate) {
        [_chatdelegate onChat:inputField.text];
    }
    [inputField setText:@""];
}

-(void)keyboardWasChange:(NSNotification *)notification
{
    NSLog(@"notification %@ %f",notification,self.frame.size.height);
    // CGRect beginRect = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    
    CGRect inputrect  =  [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
                        floatValue];
    heightBoard = inputrect.size.height;
    if (chatstate == ttchatviewstatshowemoji || chatstate == ttchatviewstatshowmore) {
        return;
    }
    [UIView animateWithDuration:duration animations:^{
        CGFloat height = inputrect.origin.y + self.superview.frame.size.height - HEIGHT_INPUT -[UIScreen mainScreen].bounds.size.height;
        NSLog(@"notification height %f",height);
        [self.po_frameBuilder setY:height];
    } completion:^(BOOL finished) {
    }];
    CGFloat height =[UIScreen mainScreen].bounds.size.height;
    if ((inputrect.origin.y+inputrect.size.height) <= height ) {
        [self showstat:ttchatviewstatinputtext];
    }else
    {
        [self showstat:ttchatviewstatnormal];
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)initUI
{
    [self setBackgroundColor:[UIColor lightGrayColor]];
    
    CGFloat inputWidth  = [UIScreen mainScreen].bounds.size.width - 80;
    inputField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, inputWidth, 40)];
    [inputField setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:inputField];
    
    UIButton *sendbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [sendbtn setFrame:CGRectMake(0, 5, 50, 40)];
    [sendbtn setTitle:@"发送" forState:UIControlStateNormal];

    [self addSubview:sendbtn];
    [sendbtn.po_frameBuilder alignRightOfView:inputField offset:10.0f];
    
    [sendbtn addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)showstat:(NSUInteger)stat
{
    if (!isuiinit) {
        [self initUI];
        isuiinit = YES;
    }
    chatstate = stat;
    switch (chatstate) {
        case ttchatviewstatnormal:
            [self.po_frameBuilder alignToBottomInSuperviewWithInset:0.0f];
            break;
            
        default:
            break;
    }
}
@end
